﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Microsoft.Office.Interop.Excel;

namespace ExcelGraphDrawer
{
    public class ExcelHelper
    {
        public static void DrawGraph(string inputPath1, string inputPath2)
        {
            Application xlApp = new  Application();

            TextReader tr1 = new StreamReader(inputPath1);

            TextReader tr2 = new StreamReader(inputPath2);

            if (xlApp == null)
            {
                Console.WriteLine("EXCEL could not be started. Check that your office installation and project references are correct.");
                return;
            }
            xlApp.Visible = true;

            Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
            Worksheet ws = (Worksheet)wb.Worksheets[1];

            if (ws == null)
            {
                Console.WriteLine("Worksheet could not be created. Check that your office installation and project references are correct.");
            }

            // Select the Excel cells, in the range c1 to c7 in the worksheet.
            Range aRange = ws.get_Range("C1", "C7");

            if (aRange == null)
            {
                Console.WriteLine("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
            }

            string line1, line2;
            int line = 1;
            while (!string.IsNullOrEmpty(line1 = tr1.ReadLine()) && !string.IsNullOrEmpty(line2 = tr2.ReadLine()))
            {                

                var result = line1.Split(' ');
                double val1 = Double.Parse(result[0]);
                double val2 = Double.Parse(result[1]);
                ws.Cells[line, 1] = val1;
                ws.Cells[line, 2] = val2;

                result = line2.Split(' ');
                val1 = Double.Parse(result[0]);
                val2 = Double.Parse(result[1]);

                ws.Cells[line, 4] = val1;
                ws.Cells[line, 5] = val2;


                line++;
            }

            Range chartRange;

            ChartObjects xlCharts = (ChartObjects)ws.ChartObjects(Type.Missing);
            ChartObject myChart = (ChartObject)xlCharts.Add(10, 80, 300, 250);
            Chart chartPage = myChart.Chart;

            chartRange = ws.get_Range("A1", "A50");
            //chartPage.SetSourceData(chartRange, XlRowCol.xlRows);
            chartPage.ChartType = XlChartType.xlXYScatter;

            object oMissing = Type.Missing;

            SeriesCollection seriesCol =
            (SeriesCollection)chartPage.SeriesCollection(oMissing);
            Series s = seriesCol.NewSeries();
            s.XValues = ws.get_Range("A1", "A100");
            s.Values = ws.get_Range("B1", "B100");

            Series s2 = seriesCol.NewSeries();
            s2.XValues = ws.get_Range("D1", "D100");
            s2.Values = ws.get_Range("E1", "E100");
        }
    }
}
